﻿using Quartz;
using Serilog;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp_Quartz_Demo
{
    class SimpleQuartzJob : IJob
    {
        //获取日志对象
        ILogger _logger = new LoggerConfiguration()
                             .WriteTo.Console()
                             .CreateLogger();

        public async Task Execute(IJobExecutionContext context)
        {
            await Task.Delay(100);

            try
            {
                string jobName = context.JobDetail.Description;
                if (DateTime.Now.Millisecond % 3 == 0)
                {
                    throw new Exception("自定义抛出错误");
                }
                else if (DateTime.Now.Millisecond % 5 == 0)
                {
                    int a = 0;
                    a = a / a;
                }
                _logger.Information($"执行任务：{jobName} 时间：{DateTime.Now}");

            }
            catch (Exception ex)
            {
                _logger.Error("----Error in job  ex:"+ex.Message);
                JobExecutionException jobExe = new JobExecutionException(ex);
                jobExe.RefireImmediately = true;
                throw jobExe;
            }

        }
    }
}
